//**********************************************************************************
// Klasse InputTextFieldLimiter zum Filtern der Eingabe auf ganze positive Zahlen
//
//
//**********************************************************************************
package binarytree.controller;

import javax.swing.text.AttributeSet;
import javax.swing.text.BadLocationException;
import javax.swing.text.PlainDocument;


public class InputTextFieldLimiter extends PlainDocument {
    private int limit;

    public InputTextFieldLimiter(int limit) {
        super();
        this.limit = limit;
    }

    @Override
    public void insertString(int offset, String str, AttributeSet attr) throws BadLocationException {

        int value;
        try {
            value = Integer.parseInt(str);
        } catch (NumberFormatException e) {
            str = "";
        }
        if ((getLength() + str.length()) <= limit) {
            super.insertString(offset, str, attr);
        }
    }
}
